package com.fw.ui.view.settings.usercontrol.merchantuser.event;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.service.bean.SearchCriteriaBean;
import com.fw.service.bean.SearchResultBean;
import com.fw.service.merchantuser.bean.MerchantUserBean;
import com.fw.service.merchantuser.bean.SearchMerchantUserBean;
import com.fw.ui.bao.MerchantUserBAO;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.event.AbstractSizeTableValueChangeListener;
import com.fw.ui.view.settings.usercontrol.merchantuser.SearchMerchantUserView;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.BeanItem;

@Component
@Scope(SCOPE_PROTOTYPE)
public class SearchMerchantUserTableSizeValueChangeListener extends AbstractSizeTableValueChangeListener {

    private static final long serialVersionUID = 1L;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;

    @Autowired
    private MerchantUserBAO merchantUserBAO;

    @SuppressWarnings("rawtypes")
    @Override
    public void change(ValueChangeEvent event, int page, int size) throws Exception {
	SearchMerchantUserView searchMerchantUserView = (SearchMerchantUserView) this.getBasedComponent();
	SearchCriteriaBean<SearchMerchantUserBean> searchCriteriaBean = searchMerchantUserView.getSearchCriteriaBean();
	searchCriteriaBean.setPage(page);
	searchCriteriaBean.setSize(size);
	SearchResultBean<MerchantUserBean> searchResultBean = merchantUserBAO.selectMerchantUser(searchCriteriaBean);

	Integer totalPage = searchResultBean.getTotalPages();
	if (totalPage == null) {
	    totalPage = 1;
	}
	Long totalElement = searchResultBean.getTotalElements();
	if (totalElement == null) {
	    totalElement = 0l;
	}
	List<BeanItem> beanItems = searchMerchantUserView.generateTableItemBean(searchResultBean);

	searchMerchantUserView.getTablePanel().clearTableData();
	searchMerchantUserView.getTablePanel().setManualPageTable(1);
	searchMerchantUserView.getTablePanel().setTableBeanContainerDataTotalPage(beanItems, totalPage, totalElement);
    }

    @Override
    public boolean validate(ValueChangeEvent event, int page, int size) {
	return true;
    }

}
